<template>
  <my-div class="login">
    <head :title="url"></head>
    <div class="logo">
      <image style="width:238px;height:142px;" src="root:img/logo@2x.png"></image>
    </div>
    <div class="login-form">
      <div class="login-input">
        <input type="text" :placeholder="$t('wallet.id_card')" class="login-input-inner" v-model="form.name" />
      </div>
      <div class="login-input" style="margin-top:40px;">
        <input type="password" :placeholder="$t('wallet.pw')" class="login-input-inner" v-model="form.password" />
      </div>
      <div class="login-btn" @click="doLogin">
        <text style="color:#ffffff;font-size:24px;text-align: center;line-height:74px;
        ">{{$t('login.login')}}</text>
      </div>
    </div>
  </my-div>
</template>

<script>
import Utils from '../../utils/index.js'
export default {
  name: 'login',
  data() {
    return {
      form:{
        name:'',
        password:''
      }
    }
  },
  methods: {
    onLoad(){
      this.check_updater()
    },
     // 检测更新
   check_updater(){
      let app;
      var env=weex.requireModule('env')
      var updater=weex.requireModule('updater')
      var device=weex.requireModule('device')
      Utils.isIOS() ? app = 2 : app = 1
      this.$http.getSilent('index/checkupdate',{appType:1,appNumber:env.versionCode()},(res)=>{
        if(res.result){
        this.modal.confirm({message:'当前已有新版本，是否更新',okTitle:'更新',cancelTitle:'下次再说'},(val)=>{
          if(val === '更新'){
            this.toast('AXSwap已在后台下载,请在控制栏中查看进度。')
            Utils.isIOS() ? device.openUrl(res.result) : updater.download(res.result)
          }
        })
        }
      })  
   },
    doLogin(){
      const account = this.pref.get('account')
      const account_list_keys = Object.keys(account)
      if(!this.form.name) return this.toast(this.$t('common.account_name'))
      if(!this.form.password) return this.toast(this.$t('common.account_pw'))
      if(account_list_keys.length<1) return this.toast(this.$t('common.register'))

      const current_ac = account_list_keys.filter(key => {
        const name =  account[key].user.name
        const pw = account[key].user.pw
        return name === this.form.name && pw === this.form.password

      })
      if(current_ac.length>0){
           var progress = weex.requireModule('progress')
           var navigator = weex.requireModule('navigator')
           this.pref.setString('current_user_key',current_ac[0])

           progress.show()
           setTimeout(()=>{
              progress.dismiss()
              navigator.pushFull({url:'root:pages/home/index.js',animated:true})
                // navigator.push({url:'root:pages/home/index.js',animated:true})
              this.toast('登录成功')
           },2000)
      }else {
           this.toast(this.$t('common.account_fali'))
      }

    }
  }
}
</script>
<style  scoped>
.login{
  position: absolute;
  top: 0;
  bottom: 0;
}
.logo{
  width: 750px;
  display: flex;
  justify-content: center;
  flex-direction: row;
  margin-top: 50px;
}
.login-form{
  display: flex;
  justify-content: center;
  margin-top: 158px;

}
.login-input{
  width: 608px;
  height: 74px;
  margin-left: 70px;
  border-radius: 10px;
  background-color: rgba(247,247,247,1);
}
.login-input-inner{
  font-size: 24px;
  height: 74px;
  padding-left: 40px;
}
.login-btn{
  width: 608px;
  margin-left: 70px;
  height: 74px;
  opacity: 1;
  background-color: rgba(70,120,245,1);
  margin-top: 100px;
  border-radius: 10px;
}
</style>
